#include "netdata.h"
#include "MeiAn.h"

static rt_mailbox_t netDataRecv_mail;
static rt_mailbox_t netDataSend_mail;


rt_err_t net_msg_mail_send(netDataMailDef *mail)
{	
	rt_err_t result;
	 
	result = rt_mb_send(netDataSend_mail, (rt_uint32_t)mail);
	if(result != RT_EOK)
	{
		rt_free(mail->data);
		rt_free(mail);
	}

	return result;
}


rt_err_t net_msg_mail_recv(netDataMailDef **mail)
{
	return rt_mb_recv(netDataRecv_mail,(rt_uint32_t *)&(*mail),RT_WAITING_NO);
}


static void connection_server(const char* url, int port)
{
    char *recv_data;
    struct hostent *host;
    int sock, bytes_received;
    struct sockaddr_in server_addr;
    int ret;

    host = gethostbyname(url);
		if(host == RT_NULL)
		{
			return ;
		}
    recv_data = rt_calloc(1,TCP_BUF_MAX_SIZE);
    if (recv_data == RT_NULL)
    {
        rt_kprintf("No memory\n");
        return;
    }
    if ((sock = socket(AF_AT, SOCK_STREAM, 0)) < 0)
    {
        rt_kprintf("Socket error\n");
        rt_free(recv_data);
				closesocket(sock);
        return;
    }
		rt_kprintf("create socket ok %d\n",sock);
		
    server_addr.sin_family = AF_AT;
    server_addr.sin_port = htons(port);
    server_addr.sin_addr = *((struct in_addr *) host->h_addr);
    //rt_memset(&(server_addr.sin_zero), 0, sizeof(server_addr.sin_zero));

		rt_kprintf("connect server\n");
    if (connect(sock, (struct sockaddr *) &server_addr,
                sizeof(struct sockaddr)) < 0)
    {
        rt_kprintf("Connect error\n");
				closesocket(sock);
				rt_free(recv_data);
        return;
    }
		pro_evt_send(ProDevLogin);

    while (1)
    {
        bytes_received = recv(sock, recv_data, TCP_BUF_MAX_SIZE, MSG_DONTWAIT);
				if(bytes_received >0)//接收到TCP数据
				{
					netDataMailDef *mail;
					rt_err_t result;
					
					mail = rt_calloc(1, sizeof(netDataMailDef));
					mail->len = bytes_received;
					mail->data = rt_calloc(1, mail->len);
					rt_memcpy(mail->data, recv_data, mail->len);
					
					result = rt_mb_send(netDataRecv_mail,(rt_uint32_t)mail);
					if(result != RT_EOK)
					{
						rt_kprintf("send netDataRecv_mail ERROR !!!\n");
						rt_free(mail);
						rt_free(mail->data);
					}
					rt_memset(recv_data, 0, TCP_BUF_MAX_SIZE);
				}
				else if(bytes_received < 0)
				{
					rt_kprintf("接收数据失败!\n");
					closesocket(sock);
					break;
				}
				else//没有数据接收，发送数据
				{
					netDataMailDef *mail;
					rt_err_t result;

					result = rt_mb_recv(netDataSend_mail, (rt_uint32_t *)&mail, 1);
					
					if(result == RT_EOK)
					{
						if(mail->data != RT_NULL)
						{
							ret = send(sock, mail->data, mail->len, 0);
							if(ret < 0)
							{
								rt_kprintf("发送数据失败\n");
								closesocket(sock);
								break;
							}
							rt_free(mail->data);
							rt_free(mail);
						}
					}
				}
    }
		
		rt_free(recv_data);
    return;
}


void netdata_thread_entry(void *arg)
{
	rt_kprintf("%s\n",__FUNCTION__);
	while(1)
	{
		connection_server("118.26.23.97",7779);
		rt_thread_delay(100);
	}
}


int netdata_thread_init(void)
{
	rt_thread_t thread;

	netDataRecv_mail = rt_mb_create("recvMail", 5, RT_IPC_FLAG_FIFO);
	RT_ASSERT(netDataRecv_mail);
	netDataSend_mail = rt_mb_create("sendMail", 5, RT_IPC_FLAG_FIFO);
	RT_ASSERT(netDataSend_mail);

	thread = rt_thread_create("netdata", netdata_thread_entry, RT_NULL, 
														1024, 19, 10);
	rt_thread_startup(thread);

	return 0;
}
INIT_APP_EXPORT(netdata_thread_init);



